package com.ayla.demo.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.ayla.demo.binder.FreshAirBinder
import com.ayla.demo.binder.MusicBinder
import com.ayla.demo.databinding.FragmentWidgetBinding
import com.ayla.demo.widgets.StaggeredItemDecoration
import com.ayla.demo.widgets.adapter.BinderAdapter
import com.ayla.demo.widgets.touch.TouchCallback
import com.blankj.utilcode.util.SizeUtils

class WidgetFragment : Fragment() {

    private lateinit var binding: FragmentWidgetBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentWidgetBinding.inflate(inflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val adapter = BinderAdapter()
        adapter.addItemBinder(MusicBinder())
        adapter.addItemBinder(FreshAirBinder())
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL)
        layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
        val pagerSnap = PagerSnapHelper()
        pagerSnap.attachToRecyclerView(binding.recyclerView)
        binding.apply {
            recyclerView.layoutManager = layoutManager
            val dp10 = SizeUtils.dp2px(10f)
            recyclerView.addItemDecoration(
                StaggeredItemDecoration(
                    dp10,
                    dp10,
                    dp10,
                    dp10,
                    dp10,
                    dp10
                )
            )
            recyclerView.adapter = adapter
        }
        val musicList = mutableListOf(1)
        val airList = List(20) {
            it.toString()
        }
        val list = mutableListOf<Any>().also {
            it.addAll(musicList)
            it.addAll(airList)
        }
        adapter.submitList(list)
//        val helper = ItemTouchHelper(TouchCallback(adapter))
//        helper.attachToRecyclerView(binding.recyclerView)

    }
}